package cm.example.shoppingplatformspringboot.controller;

import cm.example.shoppingplatformspringboot.entity.Order;
import cm.example.shoppingplatformspringboot.service.OrderService;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cm.example.shoppingplatformspringboot.utils.ResponseUtil.generateResponse;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    // 创建订单
    @PostMapping("/create")
    public ResponseEntity<Map<String, Object>> createOrder(@RequestBody Order order) {
        int orderId = orderService.createOrder(order);
        Map<String, Object> data = new HashMap<>();
        data.put("orderId", orderId);
        data.put("orderNo", order.getNo());
        return generateResponse("success", "订单创建成功", data);
    }

    //获取所有订单列表
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> getAllOrders() {
        List<Order> orders = orderService.getAllOrders();
        if (orders.isEmpty()) {
            return generateResponse("error", "未查询到订单", null);
        }
        return generateResponse("success", "查询成功", orders);
    }

    //根据用户ID查询订单列表
    @GetMapping("/list/{user_id}")
    public ResponseEntity<Map<String, Object>> getOrderByUserId(@PathVariable Integer user_id) {
        List<Order> orders = orderService.getOrderByUserId(user_id);
        if (orders.isEmpty()) {
            return generateResponse("error", "未查询到订单", null);
        }
        return generateResponse("success", "查询成功", orders);
    }

    // 根据订单编号查询订单详情
    @GetMapping("/no/{no}")
    public ResponseEntity<Map<String, Object>> getOrderByNo(@PathVariable String no) {
        List<Order> order = orderService.getOrderByNo(no);
        if (order == null) {
            return generateResponse("error", "未查询到订单", null);
        }
        return generateResponse("success", "查询成功", order);
    }
    // 根据订单状态查询所有订单列表
    @GetMapping("/status/{status}")
    public ResponseEntity<Map<String, Object>> getAllOrderByStatus(@PathVariable String status) {
        List<Order> orders = orderService.getAllOrderByStatus(status);
        if (orders.isEmpty()) {
            return generateResponse("error", "未查询到订单", null);
        }
        return generateResponse("success", "查询成功", orders);
    }

    // 根据订单状态查询订单列表
    @GetMapping("/{user_id}/status/{status}")
    public ResponseEntity<Map<String, Object>> getOrderByStatus(@PathVariable Integer user_id, @PathVariable String status) {
        List<Order> orders = orderService.getOrderByStatus(user_id, status);
        if (orders.isEmpty()) {
            return generateResponse("error", "未查询到订单", null);
        }
        return generateResponse("success", "查询成功", orders);
    }

    // 删除订单方法（标记为已删除）
    @DeleteMapping("/delete/{order_id}")
    public boolean deleteOrder(@PathVariable Integer order_id) {
        return orderService.deleteOrder(order_id);
    }

    // 取消订单方法（标记为已取消）
    @PutMapping("/cancel/{order_id}/{status}")
    public boolean cancelOrder(@PathVariable Integer order_id, @PathVariable String status) {
        return orderService.updateOrderStatus(order_id, status);
    }

    //修改订单的收货地址（id为订单id）
    @PutMapping("/updateAddress")
    public boolean updateAddress(@RequestBody Order order) {
        return orderService.updateAddress(order);
    }

    // 订单支付成功后更新订单支付时间
    @PutMapping("/updatePayTime")
    public int updateOrderPayTime(@RequestParam Integer order_id) {
        return orderService.updateOrderPayTime(order_id);
    }

    // 订单发货后更新订单发货时间
    @PutMapping("/updateSendTime")
    public int updateOrderSendTime(@RequestParam Integer order_id) {
        return orderService.updateOrderSendTime(order_id);
    }

    // 订单完成后更新订单完成时间
    @PutMapping("/updateCompleteTime")
    public int updateOrderCompleteTime(@RequestParam Integer order_id) {
        return orderService.updateOrderCompleteTime(order_id);
    }
}